// Nick Nissen, Mads Jakobsen og Jesper Larsen
package hc.andersen.paraden;



import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;


public class DataHelper {

    static JSONObject jObj = null;

    // Indlæser indhold fra fil i asset mappen ind i en String
    public static String readAssetFileIntoString(Context context, String assetFileName) throws Exception {
        InputStream inStream = context.getAssets().open(assetFileName);
        return DataHelper.InputStream2String(inStream);
    }

    // Indlæser indhold fra en InputStream ind i en String
    public static String InputStream2String(InputStream inStream) throws Exception   {
        char[] buffer = new char[1000];
        StringBuilder out = new StringBuilder();
        Reader in = new InputStreamReader(inStream, "UTF-8");
        int readCount;
        do {
            readCount = in.read(buffer, 0, buffer.length);
            if (readCount>0) {
                out.append(buffer, 0, readCount);
            }
        } while (readCount>=0);
        return out.toString();
    }

    // Sender GET request returner response som et jSON object
    public static JSONObject getHttpGetRequest(String urlAdress) throws Exception
    {
        URL url = new URL(urlAdress);
        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
        httpCon.setUseCaches (false);
        httpCon.setDoInput(true);
        httpCon.setDoOutput(false);

        if (httpCon.getResponseCode()==HttpURLConnection.HTTP_OK){
            InputStream inStream = httpCon.getInputStream();

            // læser input ind i en String
            String response = DataHelper.InputStream2String(inStream);

            inStream.close();
            try {
                jObj = new JSONObject(response);
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }
        }
        return jObj;
    }

    public static Map<String,String> HentBrugerData(Context context)
    {
        Map<String,String> userData = new HashMap<String, String>();
        try
        {
            FileInputStream inFileStream = context.openFileInput("hcalogin");
            InputStreamReader inReader = new InputStreamReader(inFileStream);
            BufferedReader buffReader = new BufferedReader(inReader);

            try
            {
                String text;
                while ((text = buffReader.readLine()) != null)
                {
                    String[] splitted = text.split(":");
                    userData.put(splitted[0],splitted[1]);
                }
                return userData;
            }
            catch (Exception e)
            {
                Log.v("error: ",e.getMessage());
            }
            finally
            {
                if (buffReader != null)
                {
                    buffReader.close();

                }
            }
        }
        catch (Exception e)
        {
            Log.v("error: ", e.getMessage());
        }
        return null;
    }


    public static void GemFil(String brug, String pass, int brugerid,Context context)//gemmer i en tekstfil
    {
        try
        {

            FileOutputStream outStream = context.openFileOutput("hcalogin", Context.MODE_WORLD_WRITEABLE);
            PrintWriter outTextStream = new PrintWriter(outStream);

            String TekstTilGem = "email:"+brug+"\r\n"+"pass:"+pass+"\r\n"+"id:"+brugerid;

            outTextStream.println(TekstTilGem);
            outTextStream.close();

        } catch (Exception e) {
            e.getMessage();
        }

    }

    public static Calendar JsonDatetoDate(String jsonDate){

        String date1 = jsonDate;
        //android date
        int index_plus = date1.indexOf("+");
        jsonDate = jsonDate.substring(6, index_plus-3);
        long unix_timestamp = Long.parseLong(jsonDate)+(long)50400;

        Calendar dato = Calendar.getInstance();

        dato.setTimeInMillis(unix_timestamp*1000);

        //Date date = new Date(unix_timestamp*1000);
        //1337335200

        //1337335200

        return dato;

    }


    public static ProgressDialog getDialog() {
        return dialog;
    }

    public static void setDialog(ProgressDialog dialog) {
        DataHelper.dialog = dialog;
    }

    static ProgressDialog dialog;

}
